package com.everwin.web.utils;

import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class XDOCReport {
    private static final Logger log = LoggerFactory.getLogger(XDOCReport.class);
    public static InputStream reportDoc(Map<String,String> reportData, InputStream templateFileInputStream) {

//        InputStream in = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();;
        IXDocReport report = null;
        ByteArrayInputStream swapStream =  null;
        try {
//            in = templateFileInputStream;
            report = XDocReportRegistry.getRegistry().loadReport(templateFileInputStream, TemplateEngineKind.Freemarker);
            IContext context = getReportContext(report, reportData);
            report.process(context, outputStream);
            byte[] xls = outputStream.toByteArray();
            swapStream = new ByteArrayInputStream(xls);
        } catch (IOException e) {
            log.error("文件流获取失败", e);
        } catch (XDocReportException e) {
            log.error("导出失败", e);
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
                if (templateFileInputStream != null) {
                    templateFileInputStream.close();
                }
            } catch (IOException e) {
                log.warn("文件流关闭失败", e);
            }
        }
        return  swapStream;
    }

    private static IContext getReportContext(IXDocReport report, Map<String, String> params) throws XDocReportException {
        IContext context = null;
        if (report != null) {
            context = report.createContext();
            FieldsMetadata metadata = new FieldsMetadata();
            for (Iterator iterator = params.entrySet().iterator(); iterator.hasNext(); ) {
                Map.Entry entry = (Map.Entry) iterator.next();
                String name = entry.getKey().toString();
                String value = entry.getValue().toString();
                context.put(name, value);
            }
            report.setFieldsMetadata(metadata);
        }
        return context;
    }


    public static  void main(String args[]){
        Map<String,String> reportData = new HashMap<String,String>();
        reportData.put("vin", "张三");
//        XDOCReport.reportDoc(reportData, "D:/data/xdocreport/出去库交接单.docx");
    }
}
